package com.xmut.mall.order.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xmut.mall.order.dto.PriceDetailDTO;
import com.xmut.mall.order.entity.OrderEntity;
import com.xmut.mall.order.entity.OrderItemEntity;
import com.xmut.mall.order.feign.CouponFeignService;
import com.xmut.mall.order.feign.MemberFeignService;
import com.xmut.mall.order.feign.ProductFeignService;
import com.xmut.mall.order.feign.WareFeignService;
import com.xmut.mall.order.myenum.OrderStatusEnum;
import com.xmut.mall.order.service.OrderItemService;
import com.xmut.mall.order.service.OrderService;
import com.xmut.mall.order.utils.RespBean;
import com.xmut.mall.order.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmut.mall.order.utils.PageUtils;
import com.xmut.mall.order.utils.Query;

import com.xmut.mall.order.dao.TradeDao;
import com.xmut.mall.order.entity.TradeEntity;
import com.xmut.mall.order.service.TradeService;


@Slf4j
@Service("tradeService")
public class TradeServiceImpl extends ServiceImpl<TradeDao, TradeEntity> implements TradeService {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderItemService orderItemService;

    /**
     * 获取支付相关交易参数
     *
     * @param vo
     * @return
     */
    @Override
    public RespBean tradeDetail(PayVo vo) {
        CashierParam param = new CashierParam();
        // 获取当前交易号码
        TradeEntity tradeEntity = this.getOne(new QueryWrapper<TradeEntity>().eq("sn", vo.getSn()));
        // 根据交易号码
        List<OrderEntity> orderList = orderService.list(new QueryWrapper<OrderEntity>()
                .eq("trade_sn", vo.getSn()));

        String orderSns = new String();
        for (OrderEntity orderEntity : orderList) {
            // 前置判断
            if (orderEntity.getOrderStatus().equals(OrderStatusEnum.CANCELLED)) {
                return RespBean.error("订单已取消");
            }
            if (orderEntity.getOrderStatus().equals(OrderStatusEnum.TAKE)) {
                return RespBean.error("订单未核验无法交易");
            }
            orderSns = orderSns + orderEntity.getSn() + ",";
        }
        // 根据交易SN获取订单项
        List<OrderItemEntity> orderItemList = orderItemService.list(new QueryWrapper<OrderItemEntity>().eq("trade_sn", vo.getSn()));
        // 设置title
        String tradeDetail = new String();
        for (OrderItemEntity orderItem : orderItemList) {
            tradeDetail = tradeDetail + orderItem.getGoodsName() + ",";
        }
        param.setDetail(tradeDetail);

        param.setTitle("Xmut-Mall");

        List<String> support = new ArrayList<>();
        support.add("WECHAT");
        support.add("ALIPAY");
        support.add("WALLET");
        param.setSupport(support);
        param.setPrice(tradeEntity.getFlowPrice());
        param.setOrderSns(orderSns);
        param.setCreateTime(tradeEntity.getCreateTime());
        param.setWalletValue(BigDecimal.ZERO);
        // 计算订单取消时间 订单是默认30分钟不支付就取消
        param.setAutoCancel(tradeEntity.getCreateTime().getTime() + 30 * 1000 * 60);

        return RespBean.success("获取成功", param);

    }

    /**
     * 根据订单sn获取该订单的交易sn
     * @param sn
     * @return
     */
    @Override
    public RespBean getTradeSnByOrderSn(String sn) {
        OrderEntity order = orderService.getOne(new QueryWrapper<OrderEntity>().eq("sn", sn));
        return RespBean.success("获取成功",order.getTradeSn());
    }


}